<?php
namespace Home\Controller;
use Think\Controller;
class CategoryController extends CommonController {

    protected $user_id=0;
    protected $info;
    private $cat_id = 0; // 分类id
    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();

       $this->info=$this->common_info;
       $this->cat_id=I('get.id',0, 'int');
       
       if($this->info['user_id'])
       {
         $this->user_id=$this->info['user_id'];
       }

    }

    /**
     * 获取分类信息
     * 获取顶级分类
     */
    public function get_cats() {
       /*************测试数据********************开始***/
       // $_GET['id']='0';
      //  print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

       $cat_id=I('get.id',0, 'int');


      
       // $category
        $category = D('CategoryBase')->get_one_child_tree($cat_id);

        $result=array_values($category); //返回所有键值      
        if($category)
        {
          return_data(0,'下拉商品成功',$result);
        }
        else
        {
           return_data(0,'没有商品',$result);
        }


       
    }

    /*********分类导航*********/
     public function cate_list()
     {
        $cat_id=I('cat_id',0,'int');
        $cats_list= D('categoryBase')->get_child_tree($cat_id);
        
        $cats=array();
        // if($cat_id)
        // $cats[$cat_id][]=$cat_id;

        // $fir_cat=array();//存储一级
        //二级菜单
        foreach ($cats_list as $key => $value) {
          $cats[$value['id']][]=$value['id'];
         
          foreach ($value['cat_id'] as $key2 => $value2) {
            $cats[$value['id']][]=$value2['id'];
          }
        }

     

        
      $Category=D("Category");
      foreach ($cats as $key => $value) {
         $where_str=" and g.cat_id ".db_create_in($value);
         $total_num=$Category->count_goods($where_str);//商品总数量
         $lowest_price=$Category->get_lowest_price($where_str);//商品总数量
         $cats_list[$key]['total_num']=$total_num?$total_num:0;
         $cats_list[$key]['lowest_price']=$lowest_price?price_format($lowest_price,0):price_format(0,0);
      }
      

      //去键值
      $cats_list=array_values($cats_list);
      if($cats_list)
      {
        return_data(0,'读取分类列表成功',$cats_list);
      }
      else
      {
         return_data(0,'读取分类列表失败',$cats_list);
      }
      

     } 
  

   /***下拉商品列表***/
    public function index(){
 /*************测试数据********************开始***/
     // $_POST['id']='7';
      /*************测试数据********************结束***/
      
      //配置文件
      $config=M('shop_config')->getField('code,value');
      //商品分类页列表的数量
      $limit=$config['page_size']?$config['page_size']:2;


      $last=I('post.last', 1, 'int');//页数
      $limit=I('post.amount',$limit, 'int');//大小
      $filter=array(
          'cat_id' =>I('post.id',0, 'int'),
          'good_price' =>I('post.good_price',0, 'int'),
          'sales_volume' =>I('sales_volume',0, 'int'),
          'good_click' =>I('post.good_click',0, 'int'),
          'goods_name' =>I('post.goods_name','')
        );
      $where_str='';
      $order_str='';


  // print_r(json_encode($_POST));exit();

      /***************分类条件查询******开始**********/

      if($filter['cat_id'])
      {
        $cats_list= D('categoryBase')->get_child_tree($filter['cat_id']);
        
        $cats=array();
        $cats[]=$filter['cat_id'];
        //二级菜单
        foreach ($cats_list as $key => $value) {
          $cats[]=$value['id'];
          foreach ($value['cat_id'] as $key2 => $value2) {
            $cats[]=$value2['id'];
          }
        }

        if($cats)
        {
          $where_str.=" and g.cat_id ".db_create_in($cats);
        }

      }
      


      

      /***************分类条件查询******结束**********/
      if($filter['goods_name'])
      {
        $where_str.=" and g.goods_name like '%$filter[goods_name]%' ";
      }

       /***************价格条件查询******开始**********/
       if($filter['good_price']==1)//按降序排列
       {
         $order_str.="  g.shop_price desc ";
       }
       else if($filter['good_price']==2)
       {
         $order_str.="  g.shop_price asc ";
       }
       /***************价格条件查询******结束**********/

       /***************销量条件查询******开始**********/
       else if($filter['sales_volume']==1)//按降序排列
       {
         $order_str.="  (g.sales_volume_base+xl.sales_volume) desc ";
       }
       else if($filter['sales_volume']==2)
       {
         $order_str.="  (g.sales_volume_base+xl.sales_volume) asc ";
       }
       /***************销量条件查询******结束**********/

       /***************点击商品数量条件查询******开始**********/
       else if($filter['good_click']==1)//按降序排列
       {
         $order_str.="  g.click_count desc ";
       }
       else if($filter['good_click']==2)
       {
         $order_str.="  g.click_count asc ";
       }

       /***************点击商品数量条件查询******结束**********/



      $DUsers=D('Users');

     
      $list=D('Category')->goods_list($this->info,$where_str,$limit, ($last-1)*$limit,$order_str);

      $num=D('Category')->count_goods($where_str);


      $page=get_page_list($list,$last,$limit,$num);
       
      
      
      
     if($page['list'])
      {
        return_data(0,'下拉商品成功',$page);
      }
      else
      {
         return_data(0,'没有商品',$page);
      }
    }
   
}